package com.internetCafes.spms.web.customer.common.component.msg.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.web.customer.common.component.msg.IMsgComponent;
import com.internetCafes.spms.web.customer.common.cont.CommonConst;
import com.internetCafes.spms.web.customer.common.cont.MsgConst;
import com.internetCafes.spms.web.customer.entity.area.AreaSale;
import com.internetCafes.spms.web.customer.entity.customer.Customer;
import com.internetCafes.spms.web.customer.entity.msg.Msg;
import com.internetCafes.spms.web.customer.entity.user.EntUserInfo;
import com.internetCafes.spms.web.customer.service.area.IAreaSaleService;
import com.internetCafes.spms.web.customer.service.msg.IMsgService;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import com.sms.common.util.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class MsgComponent implements IMsgComponent {

    @Autowired
    private IMsgService msgService;
    @Autowired
    private IAreaSaleService areaSaleService;

    @Override
    public void customerToInterest(Customer customer, EntUserInfo targetUser) {
        Msg msg = new Msg(MsgConst.MODULE_CODE_CUSTOMER, MsgConst.CustomerType.CUSTOMER_ADD.getId(), "\"" + customer.getName() + "\"转为意向\"");
        // 获取跟进人信息
        List<Long> receiverIdList = new ArrayList<>();
        receiverIdList.add(customer.getFollowUserId());

        List<Long> saleIdList = areaSaleService.list(new LambdaQueryWrapper<AreaSale>()
                        .eq(AreaSale::getAreaId, customer.getAreaId())
                        .eq(AreaSale::getEffectiveFlag, CommonConst.FlagEnum.IS.getId()))
                .stream()
                .map(AreaSale::getSaleId)
                .distinct()
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(saleIdList)) {
            receiverIdList.addAll(saleIdList);
        }
        msg.setReceiverIdList(receiverIdList);
        msgService.saveMsg(msg);
    }

    @Override
    public void customerToDeal(Customer customer, EntUserInfo targetUser) {
        Msg msg = new Msg(MsgConst.MODULE_CODE_CUSTOMER, MsgConst.CustomerType.CUSTOMER_ADD.getId(), "\"" + customer.getName() + "\"转为成交\"");
        // 获取跟进人信息
        List<Long> receiverIdList = new ArrayList<>();
        receiverIdList.add(customer.getFollowUserId());

        List<Long> saleIdList = areaSaleService.list(new LambdaQueryWrapper<AreaSale>()
                        .eq(AreaSale::getAreaId, customer.getAreaId())
                        .eq(AreaSale::getEffectiveFlag, CommonConst.FlagEnum.IS.getId()))
                .stream()
                .map(AreaSale::getSaleId)
                .distinct()
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(saleIdList)) {
            receiverIdList.addAll(saleIdList);
        }
        msg.setReceiverIdList(receiverIdList);
        msgService.saveMsg(msg);
    }

    @Override
    public void customerRemove(Customer customer) {
        Msg msg = new Msg(MsgConst.MODULE_CODE_CUSTOMER, MsgConst.CustomerType.CUSTOMER_ADD.getId(), "\"" + customer.getName() + "\"移除到区域名单列表\"");
        // 获取跟进人信息
        List<Long> receiverIdList = new ArrayList<>();
        receiverIdList.add(customer.getFollowUserId());

        List<Long> saleIdList = areaSaleService.list(new LambdaQueryWrapper<AreaSale>()
                        .eq(AreaSale::getAreaId, customer.getAreaId())
                        .eq(AreaSale::getEffectiveFlag, CommonConst.FlagEnum.IS.getId()))
                .stream()
                .map(AreaSale::getSaleId)
                .distinct()
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(saleIdList)) {
            receiverIdList.addAll(saleIdList);
        }
        msg.setReceiverIdList(receiverIdList);
        msgService.saveMsg(msg);
    }

    @Override
    public void customerDelete(List<Customer> customerList) {
        // 获取对应区域销售

        Map<Long, List<AreaSale>> finalSaleGroupByAreaId;
        finalSaleGroupByAreaId = getSaleListGroupByAreaIdByCustomerList(customerList);
        List<Msg> msgBatchDoList = customerList.stream()
                .map(customer -> {
                    Msg msg = new Msg(MsgConst.MODULE_CODE_CUSTOMER, MsgConst.CustomerType.CUSTOMER_TRANSFER.getId(),
                            "\"" + customer.getName() + "\"被删除\"");
                    // 获取跟进人信息
                    List<Long> receiverIdList = new ArrayList<>();
                    receiverIdList.add(customer.getFollowUserId());

                    if (finalSaleGroupByAreaId.containsKey(customer.getAreaId())) {
                        receiverIdList.addAll(finalSaleGroupByAreaId
                                .get(customer.getAreaId())
                                .stream()
                                .map(AreaSale::getSaleId)
                                .distinct()
                                .collect(Collectors.toList()));
                    }
                    msg.setReceiverIdList(receiverIdList);
                    return msg;
                }).collect(Collectors.toList());

        msgService.saveBatchMsg(msgBatchDoList);
    }

    @Override
    public void customerTransfer(List<Customer> customerList, EntUserInfo targetUser, String remark) {

        // 获取对应区域销售
        Map<Long, List<AreaSale>> finalSaleGroupByAreaId = getSaleListGroupByAreaIdByCustomerList(customerList);
        List<Msg> msgBatchDoList = customerList.stream()
                .map(customer -> {
                    Msg msg = new Msg(MsgConst.MODULE_CODE_CUSTOMER, MsgConst.CustomerType.CUSTOMER_TRANSFER.getId(),
                            "\"" + customer.getName() + "\"转让给\"" + targetUser.getNickName() + "\"" +
                                    (StrUtil.isNotBlank(remark) ? ("(" + remark + ")") : ""));
                    // 获取跟进人信息
                    List<Long> receiverIdList = new ArrayList<>();
                    receiverIdList.add(customer.getFollowUserId());
                    receiverIdList.add(targetUser.getUserId());

                    if (finalSaleGroupByAreaId.containsKey(customer.getAreaId())) {
                        receiverIdList.addAll(finalSaleGroupByAreaId
                                .get(customer.getAreaId())
                                .stream()
                                .map(AreaSale::getSaleId)
                                .distinct()
                                .collect(Collectors.toList()));
                    }
                    msg.setReceiverIdList(receiverIdList);
                    return msg;
                }).collect(Collectors.toList());

        msgService.saveBatchMsg(msgBatchDoList);
    }

    @Override
    public void customerSave(Customer customerDo, String menuName) {
        Msg msg = new Msg(MsgConst.MODULE_CODE_CUSTOMER, MsgConst.CustomerType.CUSTOMER_ADD.getId(), "\"" + customerDo.getName() + "\"新增到\"" + menuName + "\"");
        // 获取跟进人信息
        List<Long> receiverIdList = new ArrayList<>();
        receiverIdList.add(customerDo.getFollowUserId());

        List<Long> saleIdList = areaSaleService.list(new LambdaQueryWrapper<AreaSale>()
                        .eq(AreaSale::getAreaId, customerDo.getAreaId())
                        .eq(AreaSale::getEffectiveFlag, CommonConst.FlagEnum.IS.getId()))
                .stream()
                .map(AreaSale::getSaleId)
                .distinct()
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(saleIdList)) {
            receiverIdList.addAll(saleIdList);
        }
        msg.setReceiverIdList(receiverIdList);
        msgService.saveMsg(msg);
    }

    /**
     * 客户信息新增请求
     *
     * @param customerList 新增客户信息
     * @param menuName     菜单名称
     */
    @Override
    public void customerBatchSave(List<Customer> customerList, String menuName) {

        Map<Long, List<AreaSale>> finalSaleGroupByAreaId = getSaleListGroupByAreaIdByCustomerList(customerList);
        List<Msg> msgBatchDoList = customerList.stream()
                .map(customer -> {
                    Msg msg = new Msg(MsgConst.MODULE_CODE_CUSTOMER, MsgConst.CustomerType.CUSTOMER_TRANSFER.getId(),
                            "\"" + customer.getName() + "\"新增到\"" + menuName + "\"");
                    // 获取跟进人信息
                    List<Long> receiverIdList = new ArrayList<>();
                    receiverIdList.add(customer.getFollowUserId());

                    if (finalSaleGroupByAreaId.containsKey(customer.getAreaId())) {
                        receiverIdList.addAll(finalSaleGroupByAreaId
                                .get(customer.getAreaId())
                                .stream()
                                .map(AreaSale::getSaleId)
                                .distinct()
                                .collect(Collectors.toList()));
                    }
                    msg.setReceiverIdList(receiverIdList);
                    return msg;
                }).collect(Collectors.toList());

        msgService.saveBatchMsg(msgBatchDoList);
    }

    private Map<Long, List<AreaSale>> getSaleListGroupByAreaIdByCustomerList(List<Customer> customerList) {
        Map<Long, List<AreaSale>> saleGroupByAreaId = new HashMap<>();
        // 获取对应区域销售
        List<Long> areaIdList = customerList.stream()
                .map(Customer::getAreaId)
                .collect(Collectors.toList());

        if (CollectionUtil.isNotEmpty(areaIdList)) {
            saleGroupByAreaId = areaSaleService.list(new LambdaQueryWrapper<AreaSale>()
                            .in(AreaSale::getAreaId, areaIdList)
                            .eq(AreaSale::getEffectiveFlag, CommonConst.FlagEnum.IS.getId()))
                    .stream().collect(Collectors.groupingBy(AreaSale::getAreaId));
        }
        return saleGroupByAreaId;
    }
}
